import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/showRequest")
public class ShowRequest extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        // 这里设置了响应的字符编码方式和格式
        StringBuilder respBody = new StringBuilder();
        respBody.append(req.getProtocol().toString());
        System.out.println(req.getProtocol().toString());
        System.out.println(req.getProtocol());
        respBody.append("<br>");
        respBody.append(req.getMethod().toString());
        System.out.println(req.getMethod().toString());
        System.out.println(req.getMethod());
        respBody.append("<br>");
        respBody.append(req.getRequestURI().toString());
        respBody.append("<br>");
        respBody.append(req.getContextPath().toString());
        respBody.append("<br>");
        respBody.append(req.getQueryString());
        respBody.append("<br>");
        respBody.append("<h2>headers：<h2>");
        Enumeration<String> headerNames = req.getHeaderNames();
        // 这个操作的意义是获取 请求头部中的所有字段
        while(headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement().toString();
            respBody.append(headerName+" ");
            respBody.append(req.getHeaders(headerName).toString());
            respBody.append("<br>");
        }
        System.out.println(resp.toString());
        // 这里 getWriter方法 返回一个类似于套接字的对象，向这个对象写数据，就是在向网络中写数据与
        resp.getWriter().write(respBody.toString());


    }

}
